package com.chainyoung.component_tmtrade.ui.mvp.presenter;

import com.chainyoung.common.base.CommonSubscriber;
import com.chainyoung.common.exception.ApiException;
import com.chainyoung.common.mvp.BasePresenter;
import com.chainyoung.common.utils.RxUtils;
import com.chainyoung.component_tmtrade.bean.FiatCoin;
import com.chainyoung.component_tmtrade.ui.mvp.contract.TradeFiatCurrencyMainCurrencyContract;

import java.util.List;

import javax.inject.Inject;

import static com.chainyoung.common.constant.LibConstant.NO_RESULT_ERRORCODE;

public class TradeFiatCurrencyMainPresenter extends BasePresenter<TradeFiatCurrencyMainCurrencyContract.Model, TradeFiatCurrencyMainCurrencyContract.View> {


    @Inject
    public TradeFiatCurrencyMainPresenter(TradeFiatCurrencyMainCurrencyContract.Model model, TradeFiatCurrencyMainCurrencyContract.View rootView) {
        super(model, rootView);
    }


    public void getFiatCoinList() {
        addSubscribe(mModel.getFiatCoinList()
                .compose(RxUtils.<List<FiatCoin>>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<List<FiatCoin>>(mView,true) {
                    @Override
                    public void onNext(List<FiatCoin> data) {
                        mView.onGetFiatCoinListSuccess(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof ApiException) {
                            if (((ApiException) e).getErrorCode().equals(NO_RESULT_ERRORCODE)) {
                                mView.onGetFiatCoinListSuccess(null);
                            }
                            return;
                        }
                        super.onError(e);
                    }
                }));
    }


}

